/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.groupofdoom.beans;

import edu.chl.groupofdoom.entities.Comment;
import edu.chl.groupofdoom.entities.Torrent;
import edu.chl.groupofdoom.entities.User;
import edu.chl.groupofdoom.enums.Category;
import edu.chl.groupofdoom.jpa.Mapper;
import edu.chl.groupofdoom.support.SupportClass;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**
 * Controller bean for upload of torrents
 * @author Martin Agfjord
 */
@ManagedBean(name = "UploadBean")
@RequestScoped
public class UploadBean implements Serializable {

    private String name;
    private long size;
    private String location;
    private Category category;
    private List<String> msgs = new ArrayList<String>();

    /** Creates a new instance of UploadBean */
    public UploadBean() {
    }

    public boolean validation() {
        msgs.clear();
        if (name == null || name.equals("")) {
            msgs.add("Name empty");
        }
        if (size <= 0) {
            msgs.add("Size too low");
        }
        if (location == null || location.equals("")) {
            msgs.add("Location empty");
        }
        if (category == null) {
            msgs.add("Category invalid");
        }
        if (msgs.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    public String uploadTorrent() {
        if (this.validation()) {
            Torrent mTorrent = new Torrent();
            mTorrent.setCategory(category);
            mTorrent.setName(name);
            mTorrent.setLocation(new File(location));
            mTorrent.setSize(size);
            mTorrent.setDate(new Date());
            mTorrent.setComments(new ArrayList<Comment>());
            User user = SupportClass.getUserBean().getUser();
            mTorrent.setUser(user);
            Mapper.addTorrent(mTorrent);
            msgs.add("Upload complete!");
            name = "";
            size = 0;
            location = "";
        }
        return "uploadTorrent.jsf";
    }

    public List<String> getMsgs() {
        return msgs;
    }

    public void setMsgs(List<String> msgs) {
        this.msgs = msgs;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }
}
